package cn.didi.feidi.androidorder.fragment.personal;


import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.Map;

import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.api.Api;
import cn.didi.feidi.androidorder.api.ApiResponse;
import cn.didi.feidi.androidorder.api.ErrorEvent;
import cn.didi.feidi.androidorder.assembly.CommomDialog;
import cn.didi.feidi.androidorder.core.ActionCallbackListener;
import cn.didi.feidi.androidorder.core.AppActionRequest;
import cn.didi.feidi.androidorder.fragment.FeidiBaseFragment;
import cn.didi.feidi.androidorder.manager.UserManager;
import cn.didi.feidi.androidorder.util.DataCheckUtil;


public class PersonChangeFeidiNumOnceFragment extends FeidiBaseFragment {

    private static final String TAG = "ChangeFeidiNumOnceFrag";
    private TextView saveFeidiNumber;
    private EditText editFeidiNumber;

    /**
     * 与activity关联，后面可考虑单例模式
     *
     * @param bundle
     * @return
     */
    public static PersonChangeFeidiNumOnceFragment newInstance(Bundle bundle) {
        PersonChangeFeidiNumOnceFragment fragment = new PersonChangeFeidiNumOnceFragment();
        if (bundle != null)
            fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected View initFragment(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_person_change_feidi_num, container, false);
        Log.d(TAG, "initFragment");
        return rootView;
    }

    @Override
    protected void initViews(View rootView) {
        editFeidiNumber = (EditText) rootView.findViewById(R.id.person_change_person_feidi_num_edit);
        saveFeidiNumber = (TextView) rootView.findViewById(R.id.save_person_feidi_num_tv);
    }

    @Override
    protected void initData() {
        editFeidiNumber.setHint(UserManager.getFeidiAccount());
    }

    @Override
    protected void addListener() {
        if (!UserManager.getFeidiAccountStatus())
            saveFeidiNumber.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String feidiAccount = editFeidiNumber.getText().toString();
                    if (feidiAccount == null || feidiAccount.length() < 6 || feidiAccount.length() > 20) {
                        Toast.makeText(getContext(), "不能少于6位和大于20位", Toast.LENGTH_SHORT).show();
                    } else if (DataCheckUtil.isRightFeidiAccount(feidiAccount)) {
                        showConfirmDialog(feidiAccount);
                    } else {
                        Toast.makeText(getContext(), "首位为英文字母，只能包含英文字母和数字", Toast.LENGTH_SHORT).show();
                    }


                }
            });

    }

    private void showConfirmDialog(final String feidiAccount) {

        //弹出提示框
        CommomDialog dialog = new CommomDialog(getContext(), R.style.dialog, "飞滴号只能修改一次", new CommomDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if (confirm) {
                    Map<String, String> data = new HashMap<>();
                    data.put("userId", UserManager.getUserId());
                    data.put("token", UserManager.getToken());
                    data.put("feidiAccount", feidiAccount);
                    executeRequest(new AppActionRequest<String>(Api.UPDATE_USER_PERSON_INFO, data, new TypeToken<ApiResponse<String>>() {
                    }.getType(), new ActionCallbackListener<String>() {
                        @Override
                        public void onSuccess(String message, String data) {
                            // 获取到 editFeidiNumber的值并上传  设置飞滴号已设置的标志
                            Toast.makeText(getContext(), "修改成功", Toast.LENGTH_SHORT).show();
                            UserManager.setFeidiAccount(feidiAccount);
                            UserManager.setFeidiAccountStatus(true);
                            mActivity.finish();
                        }

                        @Override
                        public void onFailure(int errorEvent, String message) {
                            Log.d(TAG, "errorEvent:" + errorEvent + "message=" + message);
                            switch (errorEvent) {
                                case ErrorEvent.WRONG_FEIDI_ACCOUNT:
                                    //飞滴号格式错误或无法更改
                                    makeToast("飞滴号格式错误");
                                    break;
                                default:
                                    makeToast("捕捉到未知错误");
                                    break;
                            }
                        }
                    }));
                    //  : 2017/8/3 设置飞滴号已设置的标志 并修改
                    Toast.makeText(getContext(), "设置飞滴号", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                }
            }
        }, false, true).setPositiveButton("确定修改", "#347cf1");
        dialog.show();
    }
}
